import java.lang.annotation.Retention;
import java.util.concurrent.locks.ReentrantLock;

public class Demo4 {
    private static int count;

    public static void main(String[] args) throws InterruptedException {

        ReentrantLock locker = new ReentrantLock();

        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                locker.lock();
                try{
                    count++;
                } finally {
                    locker.unlock();
                }
//                try {
//                    locker.lock();
//                    count++;
//                } finally {
//                    locker.unlock();
//                }
//                locker.unlock();
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                locker.lock();
                try{
                    count++;
                } finally {
                    locker.unlock();
                }
            }
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println(count);
    }
}
